/*
 * @Description  : Number 实用方法
 * @Autor        : 杜康
 * @Date         : 2021-08-14 07:10:32 +0800
 * @LastEditors  : 杜康
 * @LastEditTime : 2021-08-14 22:13:25 +0800
 */

/**
 * @description: 检查数据是否为非数值
 * @param {*} value
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 07:11:14 +0800
 */
const isNaN = (value) => {
    return (
        !(typeof value === "string" || typeof value === "number") ||
        isNaN(value)
    );
};

/**
 * @description: 现金额转大写
 * @param { Number } num
 * @return { String }
 * @author: 杜康
 * @Date: 2021-08-14 08:22:24 +0800
 */
const money2Chinese = (num) => {
    let fraction = ["角", "分"];
    let digit = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
    let unit = [
        ["元", "万", "亿"],
        ["", "拾", "佰", "仟"],
    ];
    let head = num < 0 ? "欠" : "";
    num = Math.abs(num);
    let s = "";
    for (let i = 0; i < fraction.length; i++) {
        s += (
            digit[Math.floor(num * 10 * Math.pow(10, i)) % 10] + fraction[i]
        ).replace(/零./, "");
    }
    s = s || "整";
    num = Math.floor(num);
    for (let i = 0; i < unit[0].length && num > 0; i++) {
        let p = "";
        for (let j = 0; j < unit[1].length && num > 0; j++) {
            p = digit[num % 10] + unit[1][j] + p;
            num = Math.floor(num / 10);
        }
        s = p.replace(/(零.)*零$/, "").replace(/^$/, "零") + unit[0][i] + s;
    }
    return (
        head +
        s
            .replace(/(零.)*零元/, "元")
            .replace(/(零.)+/g, "零")
            .replace(/^整$/, "零元整")
    );
};

/**
 * @description: 随机数范围
 * @param { Number } min
 * @param { Number } max
 * @return { Number }
 * @author: 杜康
 * @Date: 2021-08-14 08:26:52 +0800
 */
const random = (min, max) => {
    if (arguments.length === 2) {
        return Math.floor(min + Math.random() * (max + 1 - min));
    } else {
        return null;
    }
};

/**
 * @description: 将阿拉伯数字翻译成中文的大写数字
 * @param { Number } num
 * @return { String }
 * @author: 杜康
 * @Date: 2021-08-14 08:25:55 +0800
 */
const numberToChinese = (num) => {
    let AA = new Array(
        "零",
        "一",
        "二",
        "三",
        "四",
        "五",
        "六",
        "七",
        "八",
        "九",
        "十"
    );
    let BB = new Array("", "十", "百", "仟", "万", "亿", "点", "");
    let a = ("" + num).replace(/(^0*)/g, "").split("."),
        k = 0,
        re = "";
    for (let i = a[0].length - 1; i >= 0; i--) {
        switch (k) {
            case 0:
                re = BB[7] + re;
                break;
            case 4:
                if (
                    !new RegExp("0{4}//d{" + (a[0].length - i - 1) + "}$").test(
                        a[0]
                    )
                )
                    re = BB[4] + re;
                break;
            case 8:
                re = BB[5] + re;
                BB[7] = BB[5];
                k = 0;
                break;
        }
        if (k % 4 == 2 && a[0].charAt(i + 2) != 0 && a[0].charAt(i + 1) == 0)
            re = AA[0] + re;
        if (a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k % 4] + re;
        k++;
    }

    if (a.length > 1) {
        // 加上小数部分(如果有小数部分)
        re += BB[6];
        for (let i = 0; i < a[1].length; i++) re += AA[a[1].charAt(i)];
    }
    if (re == "一十") re = "十";
    if (re.match(/^一/) && re.length == 3) re = re.replace("一", "");
    return re;
};

export default {
    isNaN,
    money2Chinese,
    random,
    numberToChinese,
};
